package com.ruoyi.system.service.impl;

import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.PurchaseDetail;
import com.ruoyi.system.mapper.PurchaseOrderMapper;
import com.ruoyi.system.domain.PurchaseOrder;
import com.ruoyi.system.service.IPurchaseOrderService;

/**
 * 采购订单Service业务层处理
 * 
 * @author 廖瀚
 * @date 2025-05-20
 */
@Service
public class PurchaseOrderServiceImpl implements IPurchaseOrderService 
{
    @Autowired
    private PurchaseOrderMapper purchaseOrderMapper;

    /**
     * 查询采购订单
     * 
     * @param id 采购订单主键
     * @return 采购订单
     */
    @Override
    public PurchaseOrder selectPurchaseOrderById(Long id)
    {
        return purchaseOrderMapper.selectPurchaseOrderById(id);
    }

    /**
     * 查询采购订单列表
     * 
     * @param purchaseOrder 采购订单
     * @return 采购订单
     */
    @Override
    public List<PurchaseOrder> selectPurchaseOrderList(PurchaseOrder purchaseOrder)
    {
        return purchaseOrderMapper.selectPurchaseOrderList(purchaseOrder);
    }

    /**
     * 新增采购订单
     * 
     * @param purchaseOrder 采购订单
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPurchaseOrder(PurchaseOrder purchaseOrder)
    {
        int rows = purchaseOrderMapper.insertPurchaseOrder(purchaseOrder);
        insertPurchaseDetail(purchaseOrder);
        return rows;
    }

    /**
     * 修改采购订单
     * 
     * @param purchaseOrder 采购订单
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePurchaseOrder(PurchaseOrder purchaseOrder)
    {
        purchaseOrderMapper.deletePurchaseDetailByOrderId(purchaseOrder.getId());
        insertPurchaseDetail(purchaseOrder);
        return purchaseOrderMapper.updatePurchaseOrder(purchaseOrder);
    }

    /**
     * 批量删除采购订单
     * 
     * @param ids 需要删除的采购订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePurchaseOrderByIds(Long[] ids)
    {
        purchaseOrderMapper.deletePurchaseDetailByOrderIds(ids);
        return purchaseOrderMapper.deletePurchaseOrderByIds(ids);
    }

    /**
     * 删除采购订单信息
     * 
     * @param id 采购订单主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePurchaseOrderById(Long id)
    {
        purchaseOrderMapper.deletePurchaseDetailByOrderId(id);
        return purchaseOrderMapper.deletePurchaseOrderById(id);
    }

    /**
     * 新增采购订单明细信息
     * 
     * @param purchaseOrder 采购订单对象
     */
    public void insertPurchaseDetail(PurchaseOrder purchaseOrder)
    {
        List<PurchaseDetail> purchaseDetailList = purchaseOrder.getPurchaseDetailList();
        Long id = purchaseOrder.getId();
        if (StringUtils.isNotNull(purchaseDetailList))
        {
            List<PurchaseDetail> list = new ArrayList<PurchaseDetail>();
            for (PurchaseDetail purchaseDetail : purchaseDetailList)
            {
                purchaseDetail.setOrderId(id);
                list.add(purchaseDetail);
            }
            if (list.size() > 0)
            {
                purchaseOrderMapper.batchPurchaseDetail(list);
            }
        }
    }
}
